package com.mvc.app.utils;

import com.mvc.app.exceptions.TypeTransformationException;

import java.lang.reflect.Constructor;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 8/27/12
 * Time: 3:40 PM
 */
public class TypeTransformer<I, O> {


    public O transform(I input, Class<O> outputClass) {

        Class<?> inputClass = input.getClass();
        try {

            Constructor<O> constructor = outputClass.getConstructor(inputClass);
            constructor.setAccessible(true);
            return constructor.newInstance(input);
        } catch (Exception e) {
            throw new TypeTransformationException("Can't obtain a " + outputClass + " from " + inputClass, e);
        }
    }
}
